package com.enjoyor.health.HGPT.web;

import cn.dev33.satoken.exception.NotLoginException;
import com.enjoyor.health.HGPT.web.RetResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

/**
 * @author Zhu Min
 * @date 2023/11/29 9:32
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    // 全局异常拦截
    @ExceptionHandler
    public RetResult handlerException(Exception e) {
        e.printStackTrace();
        log.error("服务执行异常---->{}", e.getMessage());
        return new RetResult(CodeEnum.ERROR).setResult(e.getMessage());
    }

    @ExceptionHandler(value = NotLoginException.class)
    public RetResult handlerException(NotLoginException e) {
        log.error("没有登陆---->{}", e.getMessage());
        return new RetResult(CodeEnum.TOKEN_FAILED).setResult(e.getMessage());
    }

    @ExceptionHandler(value = SQLException.class)
    public RetResult msgMySQLExecuteError(Exception e) {
        e.printStackTrace();
        log.error("Mysql执行异常");
        String message = e.getMessage();
        return new RetResult(CodeEnum.ERROR).setResult(message);
    }

    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    public RetResult msgNotFind(Exception e) {
        e.printStackTrace();
        log.error("请求错误");
        String message = e.getMessage();
        return new RetResult(CodeEnum.ERROR).setResult("请求内容未传递" + message);
    }

}
